<template>
	<view>
		<view class="navbar">
			<zbar></zbar>
			<view class="navbarcontent">
				<view class="nav_left">
					订单
				</view>
			</view>
		</view>
		<view class="tabflex" :style="{top:safeTop}">
			<view class="item" :class="chindex == item.status?'actext':''" v-for="(item,index) in tablist" :key="index"
				@click="changeIndex(item.status)">
				{{item.uname}}
				<view class="x" v-if="chindex == item.status"></view>
			</view>
		</view>
		<view class="orderlist" v-for="(item,index) in orderList" :key="index" @click="godetail(item)">
			<view class="item">
				<view class="item_top">
					<view class="dp">
						<image src="../../static/dp.png" mode=""></image>
						{{item.shopName}}
					</view>
					<view class="">
						<span :style="{
							color:item.status != '待支付' && item.status != '待取餐'
							?'#000':'#CE7034'}">{{item.status}}</span>
					</view>
				</view>
				<view class="item_cont">
					<view class="item_l">
						<image :src="item.productLogo" mode="aspectFill"></image>
						<view class="item_l_text">
							<view class="">{{item.productName}}</view>
							<view class="tipflex">
								<view class="its" v-for="(i,ix) in item.productCategory" :key="ix">{{i}}</view>
							</view>
						</view>
					</view>
					<view class="item_r">
						<view class="" style="text-align: right;">
							共{{item.productCount}}件
						</view>
						<view class="price">
							<span>￥</span>{{item.payAmount}}
						</view>
					</view>
				</view>
				<view class="item_btn">
					<view style="display: flex;align-items: center;" class="" v-if="item.status == '待支付'">
						<view style="margin-right: 20rpx;" class="b3 btn" @click.stop="handleclearOrder(item)">取消订单
						</view>
						<view class="b2 btn">继续支付</view>
					</view>
					<view class="b1 btn" v-if="item.status == '已退款' || item.status == '已完成'" @click.stop="zlyd(item)">
						再来一单</view>
					<view class="b1 btn" v-if="item.status == '待评价'" @click.stop="qpjia(item)">去评价</view>
					<view class="b2 btn" v-if="item.status == '待取餐'">取餐码</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 50rpx;">

		</view>
		<view class="kong" v-if="nolist">
			<image src="../../static/dd.png" mode=""></image>
			<view class="t1">
				尚无订单记录
			</view>
			<view class="t2">
				您可以在此查看订单历史记录
			</view>
			<view class="b1" @click="goindex">
				开始下单
			</view>
		</view>
		<uni-popup ref="popup" type="center">
			<view class="popup">
				<view class="tit">
					是否确认取消订单？
				</view>
				<view class="btns">
					<view class="b1 btn" @click="handleCanlel">取消订单</view>
					<view class="b2 btn" @click="close">我再想想</view>
				</view>
			</view>
		</uni-popup>
		<uv-tabbar :value="2" class="tab"></uv-tabbar>
	</view>
</template>

<script>
	import {
		ordersList,
		orderCancel
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				safeTop: uni.getStorageSync('statusBarHeight'),
				chindex: '',
				tablist: [{
						uname: '全部',
						status: ''
					},
					{
						uname: '待支付',
						status: '待支付'
					},
					{
						uname: '待取餐',
						status: '待取餐'
					},
					{
						uname: '待评价',
						status: '待评价'
					},
					{
						uname: '已退款',
						status: '已退款'
					}
				],

				params: {
					status: '',
					pageNum: 1,
					pageSize: 10
				},
				orderList: [],
				nolist: false
			};
		},
		onReachBottom() {
			this.params.pageNum++
			this.getlist()
		},
		onShow() {
			if (uni.getStorageSync('userToken')) {
				// this.params.status = ''
				this.params.pageNum = 1
				ordersList(this.params).then(res => {
					res.rows.forEach(item => {
						item.productCategory = item.productCategory.split(',')
					})
					this.orderList = res.rows
					let total = Math.ceil(res.total / this.params.pageSize);
					this.nolist = total == 0
				})
			}

		},
		methods: {
			close() {
				this.$refs.popup.close()
			},
			handleclearOrder(item) {
				this.id = item.id
				this.$refs.popup.open()
			},
			//删除订单
			handleCanlel() {
				orderCancel({
					id: this.id
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '订单已取消',
							icon: 'none'
						})
						this.close()
						this.params.pageNum = 1
						this.orderList = []
						this.getlist()
					}
				})
			},
			//请求数据
			getlist() {
				ordersList(this.params).then(res => {
					res.rows.forEach(item => {
						item.productCategory = item.productCategory.split(',')
					})
					this.orderList.push(...res.rows)
					let total = Math.ceil(res.total / this.params.pageSize);
					this.nolist = total == 0
					// if(this.orderList.length == res.total){
					// 	uni.showToast({
					// 		title:'已加载全部',
					// 		icon:'none'
					// 	})
					// }
				})
			},
			//标签切换
			changeIndex(i) {
				this.chindex = i
				this.params.status = i
				this.params.pageNum = 1
				this.orderList = []
				this.getlist()
			},
			//开始下单
			goindex() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			//跳转详情
			godetail(item) {
				//根据订单状态 跳转到不同的详情页
				//此处有两个区分 分别是普通详情页和 继续支付页面
				if (item.status == '待支付') {
					uni.navigateTo({
						url: `/mainpage/payjxmentorder?id=${item.id}`
					})
				} else {
					uni.navigateTo({
						url: `/mainpage/orderdetail?id=${item.id}`
					})
				}

			},


			//再来一单
			zlyd(item) {
				uni.navigateTo({
					url: `/mainpage/details?productid=${item.productId}`
				})
			},
			//评价
			qpjia(item) {
				uni.navigateTo({
					url: `/mainpage/orderevaluate?id=${item.id}`
				})
			}

		}
	}
</script>

<style lang="scss">
	page {
		background: #FBFBFB;
	}

	.navbar {
		position: sticky;
		top: 0;
		padding: 0rpx 10px;
		box-sizing: border-box;
		background-color: #fff;
		z-index: 9;

		.navbarcontent {
			position: relative;
			height: 44px;
			display: flex;
			align-items: center;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;

			.nav_left {
				display: flex;
				height: 44px;
				align-items: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 52rpx;
				color: #000000;
			}

		}


	}

	.tabflex {
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0rpx 10rpx;
		box-sizing: border-box;
		position: sticky;

		.item {
			width: 20%;
			text-align: center;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;
		}

		.actext {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #000000;
			position: relative;

			.x {
				position: absolute;
				width: 100%;
				height: 4rpx;
				background: #000000;
				bottom: -20rpx;
			}
		}

	}


	.kong {
		margin: 200rpx auto;
		text-align: center;

		image {
			width: 228rpx;
			height: 228rpx;
		}

		.t1 {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #000000;
			margin: 38rpx auto 18rpx;
		}

		.t2 {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #979797;
		}

		.b1 {
			width: 240rpx;
			height: 92rpx;
			background: #000000;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 92rpx;
			margin: 18rpx auto;
		}
	}

	.orderlist {
		.item {
			width: 750rpx;
			background: #FFFFFF;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			margin-bottom: 20rpx;
			padding: 20rpx;
			box-sizing: border-box;
			.item_top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #eee;

				span {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #CE7034;
				}

				.dp {
					display: flex;
					align-items: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #000000;
					padding-bottom: 20rpx;

					image {
						width: 32rpx;
						height: 32rpx;
						margin-right: 10rpx;
					}
				}
			}

			.item_cont {
				display: flex;
				justify-content: space-between;
				margin: 40rpx auto;

				.item_l {
					display: flex;

					image {
						width: 100rpx;
						height: 100rpx;
						border-radius: 8rpx;
					}

					.item_l_text {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #000000;
						margin-left: 16rpx;
						height: 100rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						span {
							height: 44rpx;
							background: #F6F6F6;
							border-radius: 8rpx 8rpx 8rpx 8rpx;
							padding: 0rpx 10rpx;
							font-weight: 500;
							font-size: 24rpx;
							color: #7B7B7B;
							line-height: 44rpx;
							text-align: center;
							margin-right: 10rpx;

						}

						.tipflex {
							display: flex;
							align-items: center;

							.its {
								height: 44rpx;
								background: #F6F6F6;
								border-radius: 8rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: 500;
								font-size: 24rpx;
								color: #7B7B7B;
								line-height: 44rpx;
								padding: 0rpx 16rpx;
								margin-right: 10rpx;
							}
						}
					}
				}

				.item_r {
					height: 100rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #000000;

					.price {
						font-family: 'DINB';
						font-size: 40rpx;
						color: #000000;

						span {
							font-size: 24rpx;
						}
					}
				}
			}

			.item_btn {
				display: flex;
				flex-direction: row-reverse;

				.btn {
					width: 160rpx;
					height: 64rpx;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					text-align: center;
					line-height: 64rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #000000;
				}

				.b1 {
					color: #000000;
					border: 2rpx solid #000000;
				}

				.b2 {
					color: #ffffff;
					background: #000000;
				}

				.b3 {
					color: #D9D9D9;
					border: 2rpx solid #D9D9D9;
				}
			}
		}
	}

	.popup {
		width: 630rpx;
		height: 330rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding: 80rpx 54rpx 54rpx;
		box-sizing: border-box;

		.tit {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #000000;
			text-align: center;
		}

		.btns {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 50rpx;

			.btn {
				width: 240rpx;
				height: 92rpx;
				background: #FFFFFF;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				text-align: center;
				line-height: 92rpx;
			}

			.b1 {
				color: #D9D9D9;
				border: 2rpx solid #D9D9D9;
			}

			.b2 {
				color: #ffffff;
				background: #000000;
			}
		}
	}
</style>